/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.frontend.parsers;

import leola.ast.ASTNode;
import leola.ast.Expr;
import leola.ast.NewExpr;
import leola.frontend.LeolaParser;
import leola.frontend.Token;
import leola.frontend.tokens.LeolaErrorCode;
import leola.frontend.tokens.LeolaTokenType;

/**
 * @author Tony
 *
 */
public class NewExprParser extends FuncInvocationParser {

	/**
	 * @param parser
	 */
	public NewExprParser(LeolaParser parser) {
		super(parser);
	}
	
	/* (non-Javadoc)
	 * @see leola.frontend.parsers.ExprParser#parse(leola.frontend.Token)
	 */
	@Override
	public ASTNode parse(Token token) throws Exception {
		token = nextToken(); // eat the NEW
		
		/* Native classes include '.', so we must continue until a '(' */
		String className = parseClassName(token);
		
		Expr[] params = parseActualParameters(token);
		NewExpr expr = new NewExpr(className, params);
		setLineNumber(expr, currentToken());
		return expr;
	}

	/**
	 * Parses the class name.
	 * 
	 * @param token
	 * @return
	 * @throws Exception
	 */
	private String parseClassName(Token token) throws Exception {
		String className = "";
				
		LeolaTokenType type = token.getType(); 
		while(! type.equals(LeolaTokenType.LEFT_PAREN)) {
			if ( type.equals(LeolaTokenType.IDENTIFIER) ) {
				className += token.getText();
			}
			else if ( type.equals(LeolaTokenType.DOT)) {
				className += ".";
			}
			else {
				getExceptionHandler().errorToken(token, this, LeolaErrorCode.UNEXPECTED_TOKEN);
			}
			
			token = nextToken();
			type = token.getType();
		}
		
		return className;
	}
}
